package thinking.java.initalization.and.clean.up.example;

import java.util.Arrays;
import java.util.Random;

/**
 * @author GaoFeng
 * @date 2020/1/3 11:42
 */
public class Ex_5_8_17_ObjectArrayTest {

    public static void main(String[] args) {

       Random rand = new Random(47);
       // 引用数组，创建只会默认赋值为null，不会创建对象
       StringHandler[] handlers = new StringHandler[rand.nextInt(20)];
       System.out.println("初始化完成");

       StringBuilder sb = new StringBuilder();
        // 给对象赋值,此时会触发构造器方法
        for (int i = 0;i < handlers.length;i++) {
            handlers[i] = new StringHandler(sb.append((char)('a' + i)).toString());
        }
        System.out.println(Arrays.toString(handlers));
    }

    static class StringHandler {
        String message;
        StringHandler (String message) {
            System.out.println("handler init。message： " + message);
            this.message = message;
        }
    }
}
